package com.sie.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sie.demo.entity.Role;
import com.sie.demo.entity.vo.PageRoleVo;
import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.entity.vo.QueryRoleVo;
import com.sie.demo.entity.vo.QueryUserVo;
import com.sie.demo.mapper.RoleMapper;
import com.sie.demo.mapper.UserMapper;
import com.sie.demo.service.RoleService;
import com.sie.demo.util.MyDateConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author liaozhifeng
 * @date 2021/8/4 14:29
 * @Version 1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    MyDateConverter myDateConverter;//自定义的字符串转为时间

    @Override
    public PageRoleVo selectAll(long currentPage, long pageSize, QueryRoleVo queryRoleVo) {
        //参数一是当前页，参数二是每页个数
        IPage<Role> userPage = new Page<>(currentPage, pageSize);
        IPage<Role> iPage;
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        // 没传条件来的时候直接分页根据创建时间排序查询所有的用户
        wrapper.orderByDesc("create_time");
//        wrapper.in("is_delete",0);
        //判断各参数是否有值，没有值，不管了；有值，把值加在wrapper中
        if (!StringUtils.isEmpty(queryRoleVo.getRoleName())) {
            wrapper.like("role_name", queryRoleVo.getRoleName());
        }
        if (!StringUtils.isEmpty(queryRoleVo.getEndTime())) {
            // 小于等于
            Date date = myDateConverter.convert(queryRoleVo.getEndTime());
            wrapper.le("create_time", date);
        }
        if (!StringUtils.isEmpty(queryRoleVo.getBeginTime())) {
            //大于等于
//                转换为date类型
            Date date1 = myDateConverter.convert(queryRoleVo.getBeginTime());
            wrapper.ge("create_time", date1);
        }

        if (!StringUtils.isEmpty(queryRoleVo.getStatus())) {
            int status = Integer.valueOf(queryRoleVo.getStatus());
            wrapper.in("status", status);
        }
        iPage = roleMapper.selectPage(userPage, wrapper);
        List<Role> list = iPage.getRecords();
        Long total = iPage.getTotal();
        PageRoleVo pageRoleVo = new PageRoleVo(list, total);
        return pageRoleVo;
    }

    @Override
    public Role getOne(String roleName) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_name",roleName);
        return roleMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer addRole(Role role) {
           return roleMapper.addRole(role);
    }

    @Override
    public Integer editRole(Role role) {
        UpdateWrapper updateWrapper = new UpdateWrapper<Role>();
        updateWrapper.in("role_id",role.getRoleId());
        return roleMapper.update(role,updateWrapper);
    }

    @Override
    public Integer deleteRoleByIds(String[] roleSelected) {
        List roleIds = Arrays.asList(roleSelected);
        return roleMapper.deleteBatchIds(roleIds);
    }
}
